//
//  Kratos_HttpRequst.m
//  kk_framework
//
//  Created by 7yao.top on 2018/11/7.
//  Copyright © 2018年 7yao.top. All rights reserved.
//

#import "Kratos_HttpRequst.h"
#import "AFNetworking.h"
#import "Kratos_HttpConfigure.h"


@implementation Kratos_HttpRequst
- (instancetype)init {
    self = [super init];
    if (self) {
        _Kra_requestMethod = Kratos_HttpRequestTypePost;
        _Kra_reqeustTimeoutInterval = 30.0;
        _Kra_encryptParams = @{};
        _Kra_normalParams = @{};
        _Kra_requestHeader = @{};
        _Kra_retryCount = 0;
        _Kra_apiVersion = @"1.0";
        
    }
    return self;
}

- (NSURLRequest *)Kra_generateRequest{
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    serializer.timeoutInterval = [self Kra_reqeustTimeoutInterval];
    [serializer setValue:@"http://game.lzgame.top" forHTTPHeaderField:@"Origin"];
    [serializer setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    NSMutableURLRequest *request = [serializer requestWithMethod:[self Kratos_httpMethod] URLString:[self.Kra_baseURL stringByAppendingString:self.Kra_requestURL] parameters:[self Kratos_generateRequestBody] error:NULL];
    // 请求头
//    NSMutableDictionary *header = request.allHTTPHeaderFields.mutableCopy;
//    if (!header)
//    {
//        header = [[NSMutableDictionary alloc] init];
//    }
//    [header addEntriesFromDictionary:[Kratos_HttpConfigure sharedHttpConfigure].generalHeaders];
//    request.allHTTPHeaderFields = header;
    return request.copy;
}
- (NSString *)Kratos_httpMethod{
    Kratos_HttpRequestType type = [self Kra_requestMethod];
    switch (type)
    {
        case Kratos_HttpRequestTypePost:
            return @"POST";
        case Kratos_HttpRequestTypeGet:
            return @"GET";
        default:
            break;
    }
    return @"GET";
}

/**
 公共请求参数
 
 @return 请求参数字典
 */
- (NSDictionary *)Kratos_generateRequestBody{
    NSDictionary *commonDic = [Kratos_HttpConfigure sharedHttpConfigure].Kra_generalParameters;
    NSMutableDictionary *rslt = commonDic.mutableCopy;
    if (rslt != nil) {
        [rslt addEntriesFromDictionary:self.Kra_normalParams];

    }else
    {
        return self.Kra_normalParams;
    }
    return rslt;
}




- (NSString *)Kra_baseURL{
    if (!_Kra_baseURL) {
        _Kra_baseURL = [Kratos_HttpConfigure sharedHttpConfigure].Kra_generalServer;
    }
    return _Kra_baseURL;
}

- (void)dealloc {
    if ([Kratos_HttpConfigure sharedHttpConfigure].Kra_enableDebug) {
        NSLog(@"dealloc: %@", ([self class]));
    }
}
@end
